package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsDrugDao;
import io.renren.modules.app.dao.BsDrugregisDao;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsMemberDao;
import io.renren.modules.app.entity.*;
import io.renren.modules.app.service.BsDrugregisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Service("bsDrugregisService")
@Slf4j
public class BsDrugregisServiceImpl extends ServiceImpl<BsDrugregisDao, BsDrugregisEntity> implements BsDrugregisService {

    @Resource
    private BsDrugregisDao bsDrugregisDao ;

    @Resource
    private BsDrugDao bsDrugDao ;

    @Resource
    private BsMemberDao bsMemberDao ;

    @Resource
    private BsEmployeeDao bsEmployeeDao ;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<BsDrugregisEntity> wrapper = new LambdaQueryWrapper<>();

        if (params.get("memberName") != null && !params.get("memberName").equals("")){
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsMemberEntity::getMemberName,params.get("memberName"));
            List<BsMemberEntity> bsMemberEntities = bsMemberDao.selectList(queryWrapper);
            List<String> list = new ArrayList<>();
            if (bsMemberEntities.size() > 0){
                for (BsMemberEntity bsMemberEntity : bsMemberEntities) {
                    list.add(bsMemberEntity.getMemberId());
                }
                wrapper.in(BsDrugregisEntity::getMemberId,list);
            }else {
                IPage<BsDrugregisEntity> page = this.page(
                        new Query<BsDrugregisEntity>().getPage(params),
                        new LambdaQueryWrapper<BsDrugregisEntity>().eq(BsDrugregisEntity::getId,-1)
                );
                return new PageUtils(page);
            }
        }
        if (params.get("drugregisEmployeeName") != null && !params.get("drugregisEmployeeName").equals("")){
            List<BsEmployeeEntity> entities = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .like(BsEmployeeEntity::getEmployeeName, params.get("drugregisEmployeeName")));
            List<String> list = new ArrayList<>();
            if (entities.size() > 0){
                for (BsEmployeeEntity entity : entities) {
                    list.add(entity.getEmployeeId());
                }
                wrapper.in(BsDrugregisEntity::getDrugregisEmployeeid,list);
            }else {
                IPage<BsDrugregisEntity> page = this.page(
                        new Query<BsDrugregisEntity>().getPage(params),
                        new LambdaQueryWrapper<BsDrugregisEntity>().eq(BsDrugregisEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        IPage<BsDrugregisEntity> page = this.page(
                new Query<BsDrugregisEntity>().getPage(params),
                wrapper
        );
        for (BsDrugregisEntity record : page.getRecords()) {
            //会员姓名数据清洗
            List<BsMemberEntity> entities = bsMemberDao.selectList(
                    new LambdaQueryWrapper<BsMemberEntity>()
                            .eq(BsMemberEntity::getMemberId, record.getMemberId()));
            record.setMemberName(entities.get(0).getMemberName());
            //用药陪护人员姓名清洗
            List<BsEmployeeEntity> entities1 = bsEmployeeDao.selectList(
                    new LambdaQueryWrapper<BsEmployeeEntity>()
                            .eq(BsEmployeeEntity::getEmployeeId, record.getDrugregisEmployeeid()));
            record.setDrugregisEmployeeName(entities1.get(0).getEmployeeName());
            //药品名称清洗
            List<BsDrugEntity> entities2 = bsDrugDao.selectList(
                    new LambdaQueryWrapper<BsDrugEntity>()
                            .eq(BsDrugEntity::getId, record.getDrugId()));
            record.setDrugName(entities2.get(0).getDrugName());
        }

        return new PageUtils(page);
    }

    @Override
    public List<BsDrugEntityVo> getDrug() {
        List<BsDrugEntity> entityList = bsDrugDao.selectList(null);
        List<BsDrugEntityVo> list = new ArrayList<>() ;
        for (BsDrugEntity bsDrugEntity : entityList) {
            BsDrugEntityVo bsDrugEntityVo = new BsDrugEntityVo() ;
            bsDrugEntityVo.setDrugId(bsDrugEntity.getId());
            bsDrugEntityVo.setDrugName(bsDrugEntity.getDrugName());
            list.add(bsDrugEntityVo);
        }
        return list ;
    }

    @Override
    public void saveDrugRegis(BsDrugregisEntity bsDrugregis) {
        try {
            //会员编号存在性校验
            String memberId = bsDrugregis.getMemberId();
            LambdaQueryWrapper<BsMemberEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BsMemberEntity::getMemberId,memberId);
            List<BsMemberEntity> entityList = bsMemberDao.selectList(queryWrapper);
            if (entityList.size() < 1){
                throw new RRException("请校验会员编号");
            }
            //用药陪护人员存在性校验
            String drugregisEmployeeid = bsDrugregis.getDrugregisEmployeeid();//用药陪护人员编号
            LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsEmployeeEntity::getEmployeeId,drugregisEmployeeid);
            List<BsEmployeeEntity> list = bsEmployeeDao.selectList(wrapper);
            if (list.size() < 1){
                throw new RRException("请校验用药陪护人员编号");
            }
            bsDrugregisDao.insert(bsDrugregis);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsDrugregisService --> saveDrugRegis");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateDrugRegis(BsDrugregisEntity bsDrugregis) {
        try {
            //该修改功能 限制操作人修改会员编号以及用药陪护人员编号
            String memberId = bsDrugregis.getMemberId();//会员编号
            String drugregisEmployeeid = bsDrugregis.getDrugregisEmployeeid();//用药陪护人员编号
            BsDrugregisEntity bsDrugregisEntity = bsDrugregisDao.selectById(bsDrugregis.getId());
            if (!memberId.equals(bsDrugregisEntity.getMemberId())){
                throw new RRException("会员编号不可修改");
            }
            if (!drugregisEmployeeid.equals(bsDrugregisEntity.getDrugregisEmployeeid())){
                throw new RRException("用药陪护人员编号不可修改");
            }
            bsDrugregisDao.updateById(bsDrugregis);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsDrugregisService --> updateDrugRegis");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

}
